import { toJS } from "mobx";
import { observable, action } from 'mobx';
import { Button, Spin ,message} from 'antd';

import { WeaTop, WeaTab, WeaRightMenu } from 'ecCom';
import objectAssign from 'object-assign';
import * as Apis from "../../apis/Api";

class ContractCreateStore {
    constructor(props) {
        
    }

    @observable Card = {};
    @observable error = false;
    @observable errorMsg;
    @observable tabsInfo =[{"title":"合同录入","key":"1"},{"title":"流程起草","key":"2"},{"title":"批量导入","key":"3"}];
    @observable tabKey = "1";
    @observable	value = "";
    @observable isAbc = false;
    @observable showDatas = {
		typesShow : [],
		typesCols : [],
		usedBeans : [],
		abcBtns : [],
		commonuse :'',
		user : ''
    }
    @observable	abcSelected = "";
    @observable wftypes = [];
    @observable groups = [];
    @observable contractType =[];
    @observable showCard = false;
    @observable modeInfo = {};
    @observable cardWidth = 1000;

    @action
    initContentWidth = (width)=>{
        this.cardWidth = width;
    }

    @action
    reLoad = () =>{
        this.Card = new weaCube.common.CardStore();
        this.showCard = false;
    }

    @action
    getLayoutInfo=(params)=>{
        
        Apis.getModeInfo(params).then(data => {
            if(data.status!=="0"){
                let pathStr = 'type=1&modeId='+data.modeId+'&formId='+data.formId;
                let billParams = data;
                if(typeof(billParams.addLoyoutid)!=="undefined"){
                    billParams = {...data,layoutid:billParams.addLoyoutid};
                    pathStr += '&layoutid='+billParams.addLoyoutid;
                }
                const billParams1 = {...billParams,type:1};
                
                // this.setBill(billParams1);
                this.Card.init(billParams1,this.callBack());
                // this.Card.modeExcel.containerWidth = 1500;
                this.Card.modeExcel.containerWidth = this.cardWidth;
                this.showCard = true;
                //针对建模保存完成后，根据url跳转到显示页面，需修改url，加入pathStr参数
                window.location.href=`#/main/contract/bills/create?`+pathStr;
                // window.History.push({pathname:`/main/contract/bills/create?${pathStr}`});
            }
            
        });

    }

    @action
    initTree=()=>{
        Apis.getContractType().then(data => {
            this.contractType = data.datas;
        });
    }

    @action
    changeTab = (tabKey) => {
        this.tabKey = tabKey;
    }

    @action
    setBill = (params) => {
        // const params ={type:1,modeId:1029,formId:-1606,layoutid:3456};
        // const params = { type: 1, modeId: 1011, formId: -1577};
        this.modeInfo = {...this.modeInfo,...params};
        // console.log(this.modeInfo);
        this.Card.init(this.modeInfo);
        this.Card.modeExcel.containerWidth = this.cardWidth;
        
    }

    callBack =()=>{
        message.warning('合同类型修改，布局重新加载');
    }

    @action
    initWF = () => {
        Apis.getWorkflow().then(data => {
            if(data.status==0){
                this.error = true;
                this.errorMsg = data.error;
            }else{
                this.wftypes = data.datas;
                this.groups = data.workflowType||[];
                this.setUpdate();
            }
        });
        
    }

    @action 
    initMode = () =>{
        this.Card = new weaCube.common.CardStore();
        Apis.getModeInfo({cons_type:0}).then(data => {
            if(data.status!=="0"){
                this.modeInfo = data;
            }
        });
    }


    //搜索
    @action
    setSearchValue(v){
        this.value  = v;
        this.setUpdate();
    }


    //是否abc
    @action
	setIsAbc(bool){
		this.isAbc = bool;
		this.setUpdate();
    }
    
    //设置选中按钮
    @action
    setAbcSelected(v){
        this.abcSelected = v;
        this.setUpdate();
    }

    @action 
    setUpdate(){
        const {wftypes,isAbc,showDatas,abcSelected,value} = this;
        const _value = value.toUpperCase();
        let typesShow = [];
        let abcBtns = [];
        wftypes.map(w=>{
			let wNew = objectAssign({},w);
			if(_value !== "") {
				wNew.wfbeans = wNew.wfbeans.filter(b=>{return (b.name.toUpperCase().indexOf(_value)>=0 || b.id.indexOf(_value)>=0 || b.spell.indexOf(_value)>=0) });
			}
		
			typesShow.push(wNew);
		})

        const colorarray = ["#55D2D4","#B37BFA","#FFC62E","#8DCE36","#37B2FF","#FF9537","#FF5E56"];
        if(isAbc){
    		let typesABC = [];
			for(let i=0; i<27; i++){
        		let wfBeansAbc = [];
        		typesABC.push({
        			"letter": i == 26 ? "···" : String.fromCharCode(65+i),
        			"wfbeans":function(){
	        			typesShow.map(t=>{
    						t.wfbeans.map(b=>{
								b.letter.charCodeAt(0) - 65 == i && wfBeansAbc.push(b);
								i == 26 && (b.letter.charCodeAt(0) >= 91 || b.letter.charCodeAt(0) < 65) && wfBeansAbc.push(b);
        					});
        				});
        				return wfBeansAbc;
        			}(),
        			// "color": colorarray[i%7],
        			"disabled": wfBeansAbc.length <= 0,
        			"selected": abcSelected == (i == 26 ? "···" : String.fromCharCode(65+i))
        		});
			}
			abcBtns = typesABC;
			typesShow = typesABC;
		}
        
        typesShow = typesShow.filter(s=>{
			const {wfbeans =[]} = s;
			return wfbeans.length > 0;
        });
        
		showDatas.typesShow = typesShow;
        showDatas.abcBtns = abcBtns;
        
        const docWidth = document.documentElement.clientWidth;
		let typesCols = docWidth > 1400 ? [[],[],[],[]] : (docWidth > 1100 ? [[],[],[]] : (docWidth > 600 ? [[],[]] : [[]]));
        let colHeight = docWidth > 1400 ? [0,0,0,0] : (docWidth > 1100 ? [0,0,0] : (docWidth > 600 ? [0,0] : [0]));
        
        // typesShow.length > 0 && typesShow.map(t=>{
		// 	const {wfbeans = []} = t;
		// 	if(wfbeans.length >= 0){
		// 		let minH = Math.min.apply(Math, colHeight);
	   	// 		for(let i = 0 ;i < colHeight.length;i++){
	   	// 			if(colHeight[i] == minH){
	   	// 				typesCols[i].push(t);
	   	// 				colHeight[i] += wfbeans.length + 3;
	   	// 				break;
	   	// 			}
	   	// 		}
		// 	}
		// });


        if (typesShow.length > 0) {
            for (let j = 0; j < typesShow.length; j++) {
                let { wfbeans = [] } = typesShow[j];
                typesShow[j].color = colorarray[j % 7];
                if (wfbeans.length >= 0) {
                    let minH = Math.min.apply(Math, colHeight);
                    for (let i = 0; i < colHeight.length; i++) {
                        if (colHeight[i] == minH) {
                            typesCols[i].push(typesShow[j]);
                            colHeight[i] += wfbeans.length + 3;
                            break;
                        }
                    }
                }
            }
        }

		showDatas.typesCols  = typesCols;

        this.showDatas = {...this.showDatas,...showDatas};
    }

    @action
    changeGroup = (groupId,wfId) => {
        const params = {groupid:groupId,workflowid:wfId};
        Apis.updateGroup(params).then(data => {
            this.initWF();
        });
    }

    
}

export default ContractCreateStore;